package com.chunqiu.report.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.chunqiu.report.domain.ReportBjd;
import com.chunqiu.report.service.IReportBjdService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 报价单Controller
 *
 * @author 春秋
 * @date 2025-01-02
 */
@Controller
@RequestMapping("/baojiadan/baojiadan")
public class ReportBjdController extends BaseController
{
    private String prefix = "baojiadan/baojiadan";

    @Autowired
    private IReportBjdService reportBjdService;

    @RequiresPermissions("baojiadan:baojiadan:view")
    @GetMapping()
    public String baojiadan()
    {
        return prefix + "/baojiadan";
    }

    /**
     * 查询报价单列表
     */
    @RequiresPermissions("baojiadan:baojiadan:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ReportBjd reportBjd)
    {
        startPage();
        List<ReportBjd> list = reportBjdService.selectReportBjdList(reportBjd);
        return getDataTable(list);
    }

    /**
     * 导出报价单列表
     */
    @RequiresPermissions("baojiadan:baojiadan:export")
    @Log(title = "报价单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ReportBjd reportBjd)
    {
        List<ReportBjd> list = reportBjdService.selectReportBjdList(reportBjd);
        ExcelUtil<ReportBjd> util = new ExcelUtil<ReportBjd>(ReportBjd.class);
        return util.exportExcel(list, "报价单数据");
    }

    /**
     * 新增报价单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存报价单
     */
    @RequiresPermissions("baojiadan:baojiadan:add")
    @Log(title = "报价单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ReportBjd reportBjd)
    {
        return toAjax(reportBjdService.insertReportBjd(reportBjd));
    }


    /**
     * 新增下载模板报价单
     */
    @RequiresPermissions("baojiadan:baojiadan:view")
    @PostMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<ReportBjd> util = new ExcelUtil<>(ReportBjd.class);
        return util.importTemplateExcel("报价单数据");
    }


    /**
     * 新增导入数据报价单
     */
    @RequiresPermissions("baojiadan:baojiadan:add")
    @Log(title = "报价单", businessType = BusinessType.INSERT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ReportBjd> util = new ExcelUtil<>(ReportBjd.class);
        List<ReportBjd> reportBjdList = util.importExcel(file.getInputStream());
        String message = reportBjdService.importReportBjd(reportBjdList, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    /**
     * 修改报价单
     */
    @RequiresPermissions("baojiadan:baojiadan:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ReportBjd reportBjd = reportBjdService.selectReportBjdById(id);
        mmap.put("reportBjd", reportBjd);
        return prefix + "/edit";
    }

    /**
     * 修改保存报价单
     */
    @RequiresPermissions("baojiadan:baojiadan:edit")
    @Log(title = "报价单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ReportBjd reportBjd)
    {
        return toAjax(reportBjdService.updateReportBjd(reportBjd));
    }

    /**
     * 删除报价单
     */
    @RequiresPermissions("baojiadan:baojiadan:remove")
    @Log(title = "报价单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(reportBjdService.deleteReportBjdByIds(ids));
    }
}
